package com.reguang.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.reguang.mapper.SysMenuMapper;
import com.reguang.pojo.SysMenuDO;
import com.reguang.pojo.SysRoleMenuDO;
import com.reguang.pojo.TreeDO;
import com.reguang.service.SysMenuService;
import com.reguang.utils.BuildTree;
import com.reguang.utils.Tree;

/**
 * 
 * Title: SysMenuServiceImpl 实现类
 * <p>
 * Description:
 * </p>
 * 
 * @author 路汉腾 2020年2月1日 上午11:25:18
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {

	@Autowired
	private SysMenuMapper sysMenuMapper;

	@Override
	public List<Tree<SysMenuDO>> listMenuTree(String userId) {
		List<Tree<SysMenuDO>> trees = new ArrayList<Tree<SysMenuDO>>();
		List<SysMenuDO> menuDOs = sysMenuMapper.listMenuByUserId(userId);
		for (SysMenuDO sysMenuDO : menuDOs) {
			Tree<SysMenuDO> tree = new Tree<SysMenuDO>();
			tree.setId(sysMenuDO.getId());
			tree.setParentId(sysMenuDO.getParentId().toString());
			tree.setText(sysMenuDO.getName());
			Map<String, Object> attributes = new HashMap<>(16);
			attributes.put("url", sysMenuDO.getUrl());
			attributes.put("icon", sysMenuDO.getIcon());
			tree.setAttributes(attributes);
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		List<Tree<SysMenuDO>> list = BuildTree.buildList(trees, "0");
		return list;
	}

	@Override
	public Set<String> listPerms(String userId) {
		List<String> perms = sysMenuMapper.listUserPerms(userId);
		Set<String> permsSet = new HashSet<>();
		for (String perm : perms) {
			if (StringUtils.isNotBlank(perm)) {
				permsSet.addAll(Arrays.asList(perm.trim().split(",")));
			}
		}
		return permsSet;
	}

	@Override
	public List<SysMenuDO> selectListMenuInfo() {
		List<SysMenuDO> list = sysMenuMapper.selectListMenuInfo();
		return list;
	}

	@Override
	public int saveMenuInfo(SysMenuDO menuDo) {
		int row = sysMenuMapper.insertSelective(menuDo);
		return row;
	}

	@Override
	public int removeMenu(SysMenuDO menuDo) {
		int row = sysMenuMapper.delete(menuDo);
		return row;
	}

	@Override
	public SysMenuDO selectMenuInfo(String id) {

		SysMenuDO record = sysMenuMapper.selectById(id);
		if (record != null) {
			SysMenuDO parentDo = sysMenuMapper.selectById(record.getParentId());
			if (parentDo != null) {
				record.setParentName(parentDo.getName());
			} else {
				record.setParentName("根目录菜单");
			}
		}
		return record;
	}

	@Override
	public int editMenuInfo(SysMenuDO menuDo) {
		return sysMenuMapper.editMenuInfoById(menuDo);
	}

	/**
	 * roleId=0 为新增， 修改状态需要查询出关联信息
	 */
	@Override
	public List<TreeDO> selectListTree() {
		List<TreeDO> listTree = sysMenuMapper.selectListTree();
//		if (roleId == 0) {
//			return listTree;
//		}
//		List<SysRoleMenuDO> listRoleMenu = sysMenuMapper.selectListRoleMenuInfoById(roleId);
//		if (listRoleMenu != null) {
//          for(SysRoleMenuDO roleMenuDo : listRoleMenu) {
//        	  for(TreeDO treeDo : listTree) {
//        		  if(treeDo.getId()==Integer.parseInt(roleMenuDo.getMenuId())) {
//        			  treeDo.setState("1");
//        		  }
//        	  }
//          }
//		}
		return listTree;
	}

	/* (non-Javadoc)
	 * @see com.reguang.service.SysMenuService#selectRoleMenuRelation(java.lang.Integer)
	 */
	@Override
	public Integer[] selectRoleMenuRelation(Integer roleId) {
		Integer[] menuIds=sysMenuMapper.selectListRoleMenuInfoById(roleId);
		return menuIds;
	}

}
